const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userImgUrl:'',
    userName:'',
    wxVal:'',
    region:"",
    areaShow:false,
    userInfo:'',
    enterPriseAuth:'', //公司认证信息
    realNameAuth:'', //实名认证信息
    realNameStatus:['认证中','认证成功','认证失败','待认证'],
    cascaderValue:'',
    //地址
    areaList:[],
    columns: [],
    id:'',
    subid:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userName: options.userName,
      userImgUrl: options.avatar,
    })
    this.authInfo();
    this.areaList()
  },

  onFinish(e){
    console.log(e,'eeeeeeeeeeeeeeeeeeee')
  },

  // r认证信息
  authInfo:function(){
    var _this = this;
    wx.request({
      url: app.globalData.baseUrl + 'auth/reAuthInfo',
      data: {
        token: app.globalData.token
      },
      success(res) {
        if (res.data.code == 1000) {
          _this.setData({
            realNameAuth:res.data.data.realnameAuth,
            enterPriseAuth:res.data.data.enterPriseAuth
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  // 我的信息
  myInfo: function () {
    var _this = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'user/getUserInfo1',
      data: {
        token: app.globalData.token
      },
      success(res) {
        wx.hideLoading()
        if (res.data.code == 1000) {
          let data = res.data.data
          _this.setData({
            wxVal: data.wechat,
            id:data.province,
            subid:data.city,
            region:data.city_name,
            userInfo:data,
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  // 保存
  saveBtn:function(){
    var {  wxVal, userImgUrl,id , subid , } = this.data;

    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'user/updateUserInfo1',
      data: {
        avatar: userImgUrl,
        //qq: qqVal,
        wechat: wxVal,
        token: app.globalData.token,
        province:subid,
        city:id,
      },
      method:"POST",
      header:{
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.code == 1000) {
          wx.showToast({
            title: '保存成功',
            icon: 'success',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  /**
   * 获取地址列表
   */
  areaList(){
    let _this = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'task/district',
      data: {},
      success(res) {
        wx.hideLoading()
        if (res.data.code == 1000) {
          let list=_this.areaListHand(res.data.data)
          let listKey=Object.keys(list)
          let columns=[
            {
              values: listKey,
              className: 'column1',
            },
            {
              values: list[listKey[0]],
              className: 'column2',
              defaultIndex: 2,
            },
          ]
          console.log(columns,'columns------------------------------------')
          _this.setData({
            areaList: list,
            columns,
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  areaListHand(data) {
    let list = {}
    data.forEach(item=>{
      list[item.name]=item.child
    })
    return list
  },
  onChange(event) {
    const { picker, value, index } = event.detail;
    picker.setColumnValues(1, this.data.areaList[value[0]]);
  },

  // 选择地址
  areaBtn: function () {
    this.setData({
      areaShow:true
    })
  },

  areaCancel(){
    this.setData({
      areaShow:false
    })
  },

  areafirm(e){
    let data=e.detail.value
    let regionData
    regionData=data[0]+'-'+data[1].name
    this.setData({
      id:data[1].id,
      subid:data[1].upid,
      region:regionData,
    })
    this.areaCancel()
  },

  // 更换头像
  upImg:function(){
    var _this = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        wx.showLoading({
          title: '加载中',
        })
        wx.uploadFile({
          url: app.globalData.baseUrl + 'fileUpload',
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            token: app.globalData.token,
            file: tempFilePaths[0]
          },
          success(upres) {
            wx.hideLoading()
            var obj = JSON.parse(upres.data)
            _this.setData({
              userImgUrl: obj.data.url
            })
          }
        })
      }
    })
  },

  // 0：验证中 1：成功 2：失败 3.未认证
  // 跳转到实名认证
  torealname:function(){
    var { realNameAuth } = this.data
    let realNameAuthData = JSON.stringify(realNameAuth)
    if(realNameAuth.status==3){
      wx.navigateTo({
        url: '/userPage/pages/nameCertification/nameCertification',
      })
    }else {
      wx.navigateTo({
        url: '/userPage/pages/attestationResult/attestationResult?realNameAuth=' + realNameAuthData,
      })
    }


  },
  torealComapny(){
    let { enterPriseAuth } = this.data
    let enterPriseAuthData = JSON.stringify(enterPriseAuth)
    if(enterPriseAuth.status==3){
      wx.navigateTo({
        url: '/userPage/pages/companyAttestation/companyAttestation',
      })
    }else {
      wx.navigateTo({
        url: '/userPage/pages/attestationResult/attestationResult?enterPriseAuth=' + enterPriseAuthData,
      })
    }
  },
  // 修改登录密码
  modifyPassword:function(){
    wx.navigateTo({
      url: '/userPage/pages/modifyAccountPassword/modifyAccountPassword',
    })
  },
  // 修改钱包密码
  modifyPayPassword: function () {
    wx.navigateTo({
      url: '/userPage/pages/modifyPayPassword/modifyPayPassword',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      this.myInfo()
    this.authInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
